import os
import matplotlib.pyplot as plt
from pandas import np


def file_name(file_dir):
    for root, dirs, files in os.walk(file_dir):
        print("-----------")
        print(root)   #os.walk()所在目录
        print(dirs)   #os.walk()所在目录的所有目录名
        print(files)   #os.walk()所在目录的所有非目录文件名
        print(" ")
        return files
def read(path):
    with open(path,'r',encoding='utf-8') as f:
        data=f.read()
        data=data.split('\n')
        for i in range(data.__len__()):
            data[i]=data[i].split(',')
    data.pop(0)
    return data
def write(thePath,data,dim=2,gap=','):
    '''

    :param thePath:
    :param data:二维数组
    :return:
    '''
    if dim==2:
        with open(thePath, 'w', encoding='utf-8') as f:
            length=data.__len__()
            for i in range(length):
                line=[str(x) for x in data[i]]
                f.writelines(gap.join(line))
                if i!=length-1:
                    f.writelines("\n")
    if dim==1:
        with open(thePath, 'w', encoding='utf-8') as f:
            data=[str(x) for x in data]
            f.writelines(gap.join(data))
    return True
def floatArray(data,dim=2):
    answer=[]
    for i in range(data.__len__()):
        if dim==2:
            answer.append([float(x) for x in data[i]])
        if dim==1:
            answer.append(float(data[i]))
    return answer
def intArray(data,dim=2):
    answer=[]
    for i in range(data.__len__()):
        if dim == 2:
            answer.append([int(x) for x in data[i]])
        if dim == 1:
            answer.append(int(data[i]))
    return answer
def curve(title,x,y,label='first',x1=[],y1=[],label1='second',x2=[],y2=[],label2='third',x3=[],y3=[],label3='forth',prompt="曲线如图所示"):
    #网格
    '''
    ax = plt.gca()
    ax.set_xlim(0, x.__len__())
    miloc = plt.MultipleLocator(1)
    ax.xaxis.set_minor_locator(miloc)
    ax.grid(axis='x', which='minor')
    '''
    #网格

    plt.plot(x,y,label=label)
    plt.plot(x1,y1,label=label1)
    plt.plot(x2,y2,label=label2)
    plt.plot(x3,y3,label=label3)
    plt.title(title)
    plt.legend()

    plt.show()
    print()
    print(title,prompt)
if __name__ == '__main__':
    root='D:\大二下\数据科学\带作业\相关性'
    os.chdir(root)
    files=file_name(root)
    for i in range(files.__len__()):
        if i%2==1:
            data=read(root+'\\'+files[i])
            data=floatArray(data)
            data=np.array(data)
            curve('correlation of '+files[i][:-8],data[:,0],data[:,1],'ave',data[:,0],data[:,2],'squ')
            print(data[:,1])

